<?php
if( !defined('IN_INDEX') )
{
	die('This page is hidden.');
}
if(!checkSession())
{
	function redirect($url)
			{
			?>
				<script type="text/javascript">
					{
					window.location = "<?=$url?>";
					}
				</script>
			<?php 
			}
			redirect("index.php?action=login");
	exit;
}
else
{
	if(isset($_REQUEST['id']))
	{
		$USER_PROFILE = new User('id', $_REQUEST['id']);
		$page_url = "index.php?action=profile&id=" . $_REQUEST['id'] . "&sub=stats";
	}
	else
	{
		$USER_PROFILE = $USER;
		$page_url = "index.php?action=profile&sub=stats";
	}
}
include('inc/functions_blog.php');
include('Charts/Includes/FusionCharts.php');
$blogs = getTrainingList(" WHERE user_id='" . $USER_PROFILE->getId() . "'");
$dateformat = $CONFIG['date_format'];


?>
<SCRIPT LANGUAGE="Javascript" SRC="Charts/FusionCharts.js"></SCRIPT>
<div id='postpath'>
		<a title="<?php echo translate('gotofrontpage'); ?>" href="index.php"><?php echo translate('home'); ?></a>
		 &gt; <a href="index.php?action=blogs" title='<?php echo translate('showallblogs'); ?>'><?php echo translate('blog'); ?></a> 
         &gt; <a href="index.php?action=profile&id=<?php echo $USER_PROFILE->getId(); ?>"><?php echo ucf($USER_PROFILE->getFirstname()) . " " . ucf($USER_PROFILE->getLastname()); ?></a>
		&gt; <a href="index.php?action=profile&sub=stats&id=<?php echo $USER_PROFILE->getId(); ?>" title='<?php echo translate('stats'); ?>'><?php echo translate('stats'); ?></a>
        &gt; <a href="index.php?action=profile&sub=stats_general&id=<?php echo $USER_PROFILE->getId(); ?>" title='<?php echo translate('general'); ?>'><?php echo translate('general'); ?></a>
</div>
<?php 

if(count($blogs) > 0)
{
$strXML = "<chart caption='" . ucf($w_metric) . " ". translate('headeruservswp') ."' showBorder='1' formatNumberScale='0' baseFont='Arial' exportEnabled='1' exportShowMenuItem='1' exportAtClient='0' showExportDialog='1' exportHandler='Charts/ExportHandlers/PHP/FCExporter.php' exportAction='download' exportTargetWindow='_blank'>";

// Total kg lifted user
$time_start = mktime(0, 0, 0, 1, 1, 2000);
$time_end = mktime(23, 59, 59, 12, 31, 2020);
// $blogs = getTrainingList(" WHERE user_id='" . $USER_PROFILE->getId() . "' AND date > " . $time_start . " AND date < " . $time_end . " ORDER BY -date");

$blogs = getTrainingList(" WHERE user_id='" . $USER_PROFILE->getId() . "' ");
$results = array();
$month = 0;
$total_weight = 0;
$total_lifts = 0;
foreach($blogs as $k => $v)
	{
		$year = date("Y", $v['date']);
		$month = date("m", $v['date']);
		$day = date("d", $v['date']);
		
		$weight = 0;
		foreach($v['lifts'] as $key => $value)
		{
			$weight += r_nr(calcFromKg($value['kg']));
			$total_weight += r_nr(calcFromKg($value['kg'])) * $value['reps'];
			$total_lifts += $value['reps'];
		}
		if(array_key_exists($year, $results) && array_key_exists($month, $results[$year]) && array_key_exists($day, $results[$year][$month]))
		{
			$results[$year][$month][$day] = $results[$year][$month][$day] + $weight;
		}
		else
		{
			$results[$year][$month][$day] = $weight;
		}
	}

// Total kg lifted all users
if($w_metric == "kg")
{
	$sql = "SELECT SUM(rep * kg) AS sum_kg FROM " . LIFT_TABLE . "";
	
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	while($row = mysql_fetch_array($result))
	{
	$kglifted = round($row['sum_kg'], 2);
	$removecuruser = ($kglifted)-($total_weight);
	$strXML .= "<set label='" . translate('header_text') . "' value='" . $removecuruser . "' />";
	}
}
else
{
	$sql = "SELECT SUM(rep * kg) AS sum_kg FROM " . LIFT_TABLE . "";
	
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	while($row = mysql_fetch_array($result))
	{
	$lbslifted = round($row['sum_kg'], 2);
	$lbsliftedtotal = round($lbslifted*2.20462262, 2);
	$removecuruser2 = ($lbsliftedtotal)-($total_weight);
	$strXML .= "<set label='" . translate('header_text') . "' value='" . $removecuruser2  . "' />";
	}
}

$strXML .= "<set label='" . getNameFromId($USER_PROFILE->getId()) . "' value='" . $total_weight . "' />";
$strXML .= "</chart>";
?>
<br />
<h2><?php echo translate('stats') . " " . translate('after') . " "; ?>
<?php 
if(countTrainingsU($USER_PROFILE->getId()) > 0) 
{
	echo countTrainingsU($USER_PROFILE->getId()) . " " . translate('trainings'); 
}
?> 
<?php 
if(countMeets($USER_PROFILE->getId()) == 1) 
{
	echo translate('and') . " " . countMeets($USER_PROFILE->getId()) . " " . translate('meet'); 
}
elseif(countMeets($USER_PROFILE->getId()) >= 2)
{
	echo translate('and') . " " . countMeets($USER_PROFILE->getId()) . " " . translate('meets'); 
}
?>
</h2>
<br />
<?php echo ucf(translate('lifted')); ?> <span style="font-size:18px;"><b><?php $totalkglifted = total_kg_lifted($USER_PROFILE->getId());?><?php echo $w_metric; ?></b></span> <?php echo strtolower(translate('totally')); ?>
 <?php echo translate('on');?>  <span style="font-size:18px;"><b><?php echo countSetsUser($USER_PROFILE->getId()); ?> <?php echo translate('set'); ?></b></span> <?php echo translate('and'); ?>  <span style="font-size:18px;"><b><?php echo countRepsUser($USER_PROFILE->getId()); ?> <?php echo translate('reps'); ?></b></span> <?php echo translate('and'); ?> <?php echo translate('ithas'); ?> <?php echo translate('taken'); ?> <?php echo translate('ca'); ?>. <span style="font-size:18px;"><b><?php echo countTimeHoursUser($USER_PROFILE->getId()); ?> <?php echo strtolower(translate('hours')); ?></b></span>.
<br /><br />
<?php
echo "<h2>" . ucf($w_metric) . " " . translate('headeruservswp') . "</h2><br />";
echo renderChart("Charts/Pie3D.swf", "", $strXML, "".ucf(translate('general'))."", 900, 400, false, true);
?>
<?php
}
else
{
  echo "<br /><br /><br /><br /><br /><br /><br /><br /><br />
		<center><h2>" . translate('addanewtrainingtostart') . "</h2></center>";
}
?>